﻿using apitest.Application.Entities.Pms;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.Extensions.DependencyInjection;
using MiniExcelLibs;
using Newtonsoft.Json;
using Synology;
using Synology.Api.Auth;
using Synology.Api.Auth.Parameters;
using Synology.Classes;
using Synology.DownloadStation.Task.Parameters;
using Synology.FileStation.FileShare.Parameters;
using Synology.FileStation.VirtualFolder.Parameters;
using Synology.Interfaces;
using System;
using System.DirectoryServices.Protocols;
using System.Drawing;

namespace apitest.Application.Test.Services
{
    [DynamicApiController]
    public class Test7Service
    {
        ISqlSugarClient _db;
        public Test7Service(ISqlSugarClient db)
        {
            _db = db;
        }

        //public async Task<dynamic> T2()
        //{
        //    var list = await _db.Queryable<QC_Refer>()
        //        .ToListAsync();

        //    foreach (var item in list)
        //    {
        //        double pu = 0;
        //        double con = 0;
        //        if (double.TryParse(item.Purity, out pu))
        //        {
        //            if (pu < 1)
        //            {
        //                await _db.Updateable<QC_Refer>()
        //                    .SetColumns(o => new QC_Refer()
        //                    {
        //                        Purity = (pu * 100).ToString()
        //                    })
        //                    .Where(o => o.Id == item.Id)
        //                    .ExecuteCommandAsync();
        //            }
        //        }
        //        if (double.TryParse(item.Content, out con))
        //        {
        //            if (con < 1)
        //            {
        //                await _db.Updateable<QC_Refer>()
        //                    .SetColumns(o => new QC_Refer()
        //                    {
        //                        Content = (con * 100).ToString()
        //                    })
        //                    .Where(o => o.Id == item.Id)
        //                    .ExecuteCommandAsync();
        //            }
        //        }
        //    }
        //    return 11;
        //}

        public async Task<dynamic> T1()
        {
            var list = MiniExcel.Query<aaa>("./4444.xlsx").ToList();
            var userList = new List<string>();
            var res = new List<aaa>();
            foreach (var item in list)
            {
                if (!string.IsNullOrWhiteSpace(item.参训人员))
                {
                    item.参训人员列表 = item.参训人员.Split(" ").ToList();
                    userList.AddRange(item.参训人员列表);
                }
            }
            userList = userList.Where(o=>!string.IsNullOrWhiteSpace(o)).Distinct().ToList();
            foreach (var user in userList)
            {
                var tempList = list.Where(o => o.参训人员列表.Contains(user)).ToList();
                foreach (var item in tempList)
                {
                    item.参训人员 = user;

                    res.Add(new aaa()
                    {
                        参训人员 = user,
                        培训主题 = item.培训主题,
                        培训形式 = item.培训形式,
                        培训类型 = item.培训类型,
                        培训讲师 = item.培训讲师,
                        考核方式 = item.考核方式,
                        计划培训时间 = item.计划培训时间,
                        讲师组别 = item.讲师组别,
                    });
                }
            }
            var path = "./119.xlsx";
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            MiniExcel.SaveAs(path, res);
            return userList;
        }

        public class aaa
        {
            public string 培训类型 { get; set; }
            public string 培训主题 { get; set; }
            public string 培训形式 { get; set; }
            public string 考核方式 { get; set; }
            public string 计划培训时间 { get; set; }
            public string 讲师组别 { get; set; }
            public string 培训讲师 { get; set; }
            public string 参训人员 { get; set; }
            [MiniExcelLibs.Attributes.ExcelIgnore]
            public List<string> 参训人员列表 { get; set; }
        }

        public async Task<dynamic> T3()
        {
            ISynologyConnection connection = null;
            var settings = App.GetService<ISynologyConnectionSettings>();
            settings.BaseHost = "192.168.169.233";
            settings.Port = 5000;
            settings.Ssl = false;
            //settings.Username = "astatech";
            //settings.Password = "Nast!123";
            settings.Username = "陈佳";
            settings.Password = "Ast.123456";
            using (var syno = App.GetService<ISynologyConnection>())
            {
              await  DoConnection(syno);
               await FileStationTests(syno);
                await DownloadStationTests(syno);   
            }
            return "";
        }
        private    async Task DoConnection(ISynologyConnection syno)
        {
            Console.WriteLine("Info");
            var resInfo =await syno.Api().Info().QueryAsync();

            Console.WriteLine(JsonConvert.SerializeObject(resInfo));

            Console.WriteLine("Auth Login");
            var resLogin =await syno.Api().Auth().LoginAsync(new LoginParameters
            {
                //Username = "astatech",
                //Password = "Nast!123"
                Username = "陈佳",
                Password = "Ast.123456"
                //  Username = "ast",
                //Password = "Sr@123456"
            });

            Console.WriteLine(JsonConvert.SerializeObject(resLogin));

            //if (resLogin.Error == null || (resLogin.Error != null && resLogin.Error.Code == 403))
            //{
            //    if (resLogin.Error != null && resLogin.Error.Code == 403)
            //    {
            //        GetOtp(syno, ref resLogin);
            //    }

            //    if (resLogin.Error == null)
            //    {
            //        DownloadStationTests(syno);

            //        FileStationTests(syno);

            //        Console.WriteLine("Auth Logout");
            //        var resLogout = syno.Api().Auth().Logout();
            //        Console.WriteLine(JsonConvert.SerializeObject(resLogout));
            //    }
            //}

        }

        private async Task FileStationTests(ISynologyConnection syno)
        {
            var aa = await syno.FileStation().Upload().CgiPathAsync();
            Console.WriteLine("FS GetInfo");
            var fsResInfo =await syno.FileStation().Info().GetAsync();
            Console.WriteLine(JsonConvert.SerializeObject(fsResInfo));

            Console.WriteLine("FS File Share List Share");
            var fsResShares =await syno.FileStation().FileShare().ListShareAsync(
                FileShareDetailsType.RealPath | FileShareDetailsType.Size | FileShareDetailsType.Owner |
                FileShareDetailsType.Time | FileShareDetailsType.Perm | FileShareDetailsType.VolumeStatus |
                FileShareDetailsType.MountPointType);
            Console.WriteLine(JsonConvert.SerializeObject(fsResShares));

            Console.WriteLine("FS File Share List");
            var fsResList = await syno.FileStation().FileShare().ListAsync("/downloads", null, FileType.All, null,
                FileDetailsType.RealPath | FileDetailsType.Size | FileDetailsType.Owner | FileDetailsType.Time |
                FileDetailsType.Perm | FileDetailsType.Type | FileDetailsType.MountPointType);
            Console.WriteLine(JsonConvert.SerializeObject(fsResList));

            Console.WriteLine("FS File Share Info");
            var fsResFileInfo = await syno.FileStation().FileShare().InfoAsync("/downloads/.apdisk",
                FileDetailsType.RealPath | FileDetailsType.Size | FileDetailsType.Owner | FileDetailsType.Time |
                FileDetailsType.Perm | FileDetailsType.Type | FileDetailsType.MountPointType);
            Console.WriteLine(JsonConvert.SerializeObject(fsResFileInfo));

            Console.WriteLine("FS Virtual Folder List");
            var fsVfResList = await syno.FileStation().VirtualFolder().ListAsync(
                VirtualFolderDetailsType.RealPath | VirtualFolderDetailsType.Owner | VirtualFolderDetailsType.Time |
                VirtualFolderDetailsType.Perm | VirtualFolderDetailsType.MountPointType |
                VirtualFolderDetailsType.VolumeStatus);
            Console.WriteLine(JsonConvert.SerializeObject(fsVfResList));
        }

        private async Task DownloadStationTests(ISynologyConnection syno)
        {
        
            //Console.WriteLine("DS Info");
            //var dsResInfo =await syno.DownloadStation().Info().GetInfoAsync();
            //Console.WriteLine(JsonConvert.SerializeObject(dsResInfo));
            Console.WriteLine("DS Config");
            var dsResConfig = await syno.DownloadStation().Info().ConfigAsync();
            Console.WriteLine(JsonConvert.SerializeObject(dsResConfig));

            Console.WriteLine("DS Schedule Config");
            var dsResSchedule =await syno.DownloadStation().Schedule().ConfigAsync();
            Console.WriteLine(JsonConvert.SerializeObject(dsResSchedule));

            Console.WriteLine("DS Task List");
            var dsResTasks =await syno.DownloadStation().Task().ListAsync(new TaskListParameters
            {
                Additional = TaskDetailsType.Detail | TaskDetailsType.Transfer | TaskDetailsType.File | TaskDetailsType.Tracker | TaskDetailsType.Peer
            });
            Console.WriteLine(JsonConvert.SerializeObject(dsResTasks));
        }
    }
}
